$(document).ready(function() {
  ifLoged();
  //需要参数 订单id 支付方式 酒店名称 床位名称 总价格 等
  var sec = location.href.split("?")[1],
    d = JSON.parse(decodeURIComponent(sec)); //得到原json数据
  console.log(d);
  $(".hotelName").html(d.hotel.name);
  $(".total-price").html(d.amount);
  $(".roomName").html(d.roomName);
  var rate;

  rating.init("#userrating", {
    readOnly: false,
    mode: "lightEntire",
    num: 0,
    //单选评分之后发起ajax请求
    choosen: function(num, total) {
      rate = num;
    }
  });
  var formData = [],
    picPath = [];
  //多图上传实时预览
  $("#commentPhoto").on("change", function() {
    var $this = $(this),
      length = $this[0].files.length,
      file = [],
      filesListed = [];
    if (length == 0) {
      return;
    } else if (length > 10) {
      console.log("请上传不多于十张照片");
      return;
    }
    $(".fileinput img").remove();
    for (let i = 0; i < $this[0].files.length; i++) {
      formData[i] = new FormData();
      file[i] = $("#commentPhoto")[0].files[i];
      formData[i].append("token", localStorage.APISHoteltoken);
      formData[i].append("type", "evaluate");
      var filereader = new FileReader();
      filereader.onload = function(e) {
        if (file[i].size > 3 * 1024 * 1024) {
          alert("上传图片大小不得超过3M");
          return;
        }
        var srcpath = e.target.result;

        showPreviewImage(srcpath);
        // filesListed.push(srcpath);
      };
      filereader.readAsDataURL(file[i]);
      formData[i].append("img", file[i]);
    }

    function showPreviewImage(src) {
      var img = $('<img class="commentPic" src="' + src + '" >');
      $(".fileinput").append(img);
    }
  });

  $("#submit").on("click", function() {
    for (let i = 0; i < formData.length; i++) {
      $.ajax({
        url: url + "v1/upload/picture",
        type: "POST",
        cache: false,
        data: formData[i],
        xhrFields: {
          withCredentials: true // 允许跨域名储存和访问cookie
        },
        processData: false,
        contentType: false
        // "multipart/form-data"
      })
        .done(function(data) {
          if (data.code != "success") {
            alert(data.message);
            console.log(data.message);
          } else {
            console.log("上传第" + (i + 1) + "张图片成功!");
            picPath.push(data.data.picturePath);
          }
        })
        .fail(function() {
          alert("上传图片失败!");
          console.log("上传图片失败!");
        });
    }
    setTimeout(() => {
      var text = $("#comment").val(),
        PicArr = JSON.stringify(picPath);
      $(picPath).stringify();
      console.log(PicArr);
      ajaxXHR(
        "post",
        url + "v1/order/evaluate",
        function(data) {
          if (data.code != "success") {
            alert(data.message);
            console.log(data.message);
          } else {
            console.log(data);
            $(".modal-body").html(
              '上传图片成功!去<a href="myIndent.html">我的订单</a>页'
            );
            $(".modal").modal("show");
          }
        },
        {
          token: localStorage.APISHoteltoken,
          orderId: d._id,
          score: rate,
          content: text,
          picturePathArry: PicArr
        },
        "json"
      );
    }, 1000);

    // ajaxXHR('post', url + 'v1/order/pay', function(data) {
    //     if (data.code != 'success') {
    //         console.log(data.message);
    //     } else {
    //         console.log(data);
    //     }
    // }, {
    //     token: localStorage.APISHoteltoken,
    //     payType: $('.payItem.active')[0].dataset.value,
    //     orderId: d._id
    // }, 'json')
  });
});
$.fn.stringify = function() {
  return JSON.stringify(this);
};
